#include "console.h"

// ANSI转义序列支持
void clear_screen(void) {
    // ESC[2J - 清除整个屏幕
    // ESC[H - 光标移动到左上角
    console_puts("\033[2J\033[H");
}

void set_cursor_position(int row, int col) {
    char buf[16];
    int len = 0;
    
    buf[len++] = '\033';
    buf[len++] = '[';
    
    // 行号
    if (row >= 10) {
        buf[len++] = '0' + (row / 10);
    }
    buf[len++] = '0' + (row % 10);
    buf[len++] = ';';
    
    // 列号
    if (col >= 10) {
        buf[len++] = '0' + (col / 10);
    }
    buf[len++] = '0' + (col % 10);
    buf[len++] = 'H';
    
    console_write(buf, len);
}

void set_foreground_color(int color) {
    char buf[8];
    int len = 0;
    
    buf[len++] = '\033';
    buf[len++] = '[';
    buf[len++] = '3';
    buf[len++] = '0' + color;
    buf[len++] = 'm';
    
    console_write(buf, len);
}

void set_background_color(int color) {
    char buf[8];
    int len = 0;
    
    buf[len++] = '\033';
    buf[len++] = '[';
    buf[len++] = '4';
    buf[len++] = '0' + color;
    buf[len++] = 'm';
    
    console_write(buf, len);
}

void reset_color(void) {
    console_puts("\033[0m");
}

void clear_line(void) {
    console_puts("\033[2K");  // 清除整行
    console_puts("\033[0G");  // 光标移动到行首
}
